package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CateGoryMapper;
import com.itheima.reggie.service.CateGoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author : [wangtao]
 * @className : CateGoryServiceImpl
 * @description : []
 * @createTime : [2022/10/9 18:56]
 * @updateTime : [2022/10/9 18:56]
 */
@Service
public class CateGoryServiceImpl extends ServiceImpl<CateGoryMapper, Category> implements CateGoryService {
  @Autowired
   private SetmealService setmealService;
  @Autowired
  private DishService dishService;
/*
* 删除功能处理
* */

    @SneakyThrows
    @Override
    public void remove(Long id)  {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count1 = setmealService.count(setmealLambdaQueryWrapper);

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int count2 = dishService.count(dishLambdaQueryWrapper);
        if (count1 > 0) {
            throw new CustomException("菜品已关联套餐,无法删除");
        }
        if (count2 > 0) {
            throw new CustomException("菜品已关联菜品管理,无法删除");
        }
   super.removeById(id);
    }
}
